package com.chao.springcloud.controller;


import com.chao.springcloud.pojo.Dept;
import com.chao.springcloud.service.DeptService;
import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @HystrixCommand(fallbackMethod = "hystrixGet")
    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        Dept dept= deptService.queryById(id);
        if(dept==null){
            throw new RuntimeException("id =>" + id +"，不存在该用户，或者信息无法找到~");
//            dept=new Dept();
//            dept.setDeptno(id)
//                    .setDb_source("null")
//                    .setDname("null");
        }
        return dept;
    }

    //备选方案：熔断版本
    public Dept hystrixGet(@PathVariable("id") Long id){
        System.out.println("进入熔断方法");
        return new Dept().setDeptno(id)
                .setDname("id =>" + id +"没有对应的的信息，null-@Hystrix")
                .setDb_source("not this database");
    }
}
